﻿using System;
using System.Collections.Generic;
using UnityEngine;

// Token: 0x020000A4 RID: 164
public class MMD4MecanimMorphHelper : MonoBehaviour
{
	// Token: 0x1700009E RID: 158
	// (get) Token: 0x060003D4 RID: 980 RVA: 0x0001D79C File Offset: 0x0001B99C
	public virtual bool isProcessing
	{
		get
		{
			return (this._modelMorph != null && this._modelMorph.weight != this.morphWeight) || this._inactiveModelMorphSet.Count != 0;
		}
	}

	// Token: 0x1700009F RID: 159
	// (get) Token: 0x060003D5 RID: 981 RVA: 0x0001D79C File Offset: 0x0001B99C
	public virtual bool isAnimating
	{
		get
		{
			return (this._modelMorph != null && this._modelMorph.weight != this.morphWeight) || this._inactiveModelMorphSet.Count != 0;
		}
	}

	// Token: 0x060003D6 RID: 982 RVA: 0x0001D7D4 File Offset: 0x0001B9D4
	protected virtual void Start()
	{
		this._model = base.GetComponent<MMD4MecanimModel>();
		if (this._model != null)
		{
			this._model.Initialize();
		}
	}

	// Token: 0x060003D7 RID: 983 RVA: 0x0001D7FE File Offset: 0x0001B9FE
	protected virtual void Update()
	{
		this._UpdateMorph(Time.deltaTime);
	}

	// Token: 0x060003D8 RID: 984 RVA: 0x0001D80B File Offset: 0x0001BA0B
	public virtual void ForceUpdate()
	{
		this._UpdateMorph(0f);
	}

	// Token: 0x060003D9 RID: 985 RVA: 0x0001D818 File Offset: 0x0001BA18
	private void _UpdateMorph(float deltaTime)
	{
		this._UpdateModelMorph();
		float step = 1f;
		if (this.morphSpeed > 0f)
		{
			step = deltaTime / this.morphSpeed;
		}
		if (this._modelMorph != null)
		{
			MMD4MecanimCommon.Approx(ref this._modelMorph.weight, this.morphWeight, step);
			MMD4MecanimCommon.Approx(ref this._weight2, (!this.overrideWeight) ? 0f : 1f, step);
			this._modelMorph.weight2 = this._weight2;
		}
		else
		{
			MMD4MecanimCommon.Approx(ref this._weight2, 1f, step);
		}
		if (this._inactiveModelMorphSet != null)
		{
			foreach (MMD4MecanimModel.Morph morph in this._inactiveModelMorphSet)
			{
				MMD4MecanimCommon.Approx(ref morph.weight, 0f, step);
				MMD4MecanimCommon.Approx(ref morph.weight2, 0f, step);
			}
			this._inactiveModelMorphSet.RemoveWhere((MMD4MecanimModel.Morph s) => s.weight == 0f && s.weight2 == 0f);
		}
	}

	// Token: 0x060003DA RID: 986 RVA: 0x0001D95C File Offset: 0x0001BB5C
	private void _UpdateModelMorph()
	{
		if (this._modelMorph != null && (string.IsNullOrEmpty(this.morphName) || this._modelMorph.name != this.morphName))
		{
			if (this._modelMorph.weight != 0f || this._modelMorph.weight2 != 0f)
			{
				this._inactiveModelMorphSet.Add(this._modelMorph);
			}
			this._modelMorph = null;
		}
		if (this._modelMorph == null && this._model != null)
		{
			this._modelMorph = this._model.GetMorph(this.morphName);
			if (this._modelMorph != null && this._inactiveModelMorphSet != null)
			{
				this._inactiveModelMorphSet.Remove(this._modelMorph);
			}
		}
	}

	// Token: 0x0400053D RID: 1341
	public float morphSpeed = 0.1f;

	// Token: 0x0400053E RID: 1342
	public string morphName;

	// Token: 0x0400053F RID: 1343
	public float morphWeight;

	// Token: 0x04000540 RID: 1344
	public bool overrideWeight;

	// Token: 0x04000541 RID: 1345
	protected MMD4MecanimModel _model;

	// Token: 0x04000542 RID: 1346
	private MMD4MecanimModel.Morph _modelMorph;

	// Token: 0x04000543 RID: 1347
	private float _morphTime;

	// Token: 0x04000544 RID: 1348
	private HashSet<MMD4MecanimModel.Morph> _inactiveModelMorphSet = new HashSet<MMD4MecanimModel.Morph>();

	// Token: 0x04000545 RID: 1349
	private float _weight2;
}
